<template>
  <el-dialog
    :title="action === 'add' ? '新增标签类型' : '编辑标签类型'"
    :visible.sync="visible"
    width="50%"
    append-to-body
  >
    <avue-form
      ref="form"
      v-model="form"
      :option="insertOption"
    ></avue-form>
    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        @click="submit"
      >
        保存
      </el-button>
      <el-button @click="visible = false">取消</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { insertOption } from '@/const/crud/customer/labelMng';
import {
  insertLabel,
  updateLabel,
  selectLabelById,
} from '@/api/app/custom-label';
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    action: {
      type: String,
    },
    id: {
      type: String,
    },
  },
  data() {
    return {
      form: {},
      loading: false,
      insertOption: insertOption(),
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
  },
  watch: {
    visible(val) {
      if (val) {
        if (this.action === 'edit') {
          this.selectLabelById();
        }
      } else {
        this.form.name = '';
        this.form.sort = '';
        this.form.optionType = '';
        this.form.description = '';
        // this.$refs.form.resetFields();
      }
    },
  },
  methods: {
    selectLabelById() {
      selectLabelById(this.id).then((res) => {
        this.form = res.data.data;
      });
    },
    submit() {
      this.$refs.form.validate(async (valid, done) => {
        if (valid) {
          if (this.action === 'add') {
            this.loading = true;
            insertLabel(this.form)
              .then((res) => {
                if (!res.data) return;
                this.$emit('refresh');
                this.visible = false;
                this.loading = false;
                this.$message.success('新增成功！');
                done();
              })
              .catch((err) => {
                this.loading = false;
                done();
                throw new Error(err);
              });
          } else {
            this.loading = true;
            const params = {
              id: this.id,
              name: this.form.name,
              description: this.form.description,
              sort: this.form.sort,
              userChoiceFlag: this.form.userChoiceFlag,
              optionType: this.form.optionType,
            };
            updateLabel(params)
              .then((res) => {
                if (!res.data) return;
                this.$emit('refresh');
                this.visible = false;
                this.loading = false;
                this.$message.success('编辑成功！');
                done();
              })
              .catch((err) => {
                this.loading = false;
                done();
                throw new Error(err);
              });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
